package com.ck.print.demo.printbridge;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;

public class ConfigPropertyMapping {
    private Properties properties;

    /**
     * 初始化配置映射器
     * @param fileName properties文件名
     */
    public ConfigPropertyMapping(String fileName) {
        this.properties = properties;
        PropertiesReader reader = new PropertiesReader(fileName);
        this.properties = reader.getProperties();
    }


    /**
     * 将配置值注入到带注解的实体对象中
     * @param clazz 实体类的Class对象
     * @return 注入了配置值的实体对象
     */
    public <T> T mapToObject(Class<T> clazz) {
        try {
            T obj = clazz.getDeclaredConstructor().newInstance();

            // 遍历实体类的所有字段
            for (Field field : clazz.getDeclaredFields()) {
                // 获取字段的注解
                ConfigProperty annotation = field.getAnnotation(ConfigProperty.class);
                String configKey = annotation.value();
                String defaultValue = annotation.defaultValue();
                boolean isRequired = annotation.isRequired();
                String value = properties.getProperty(configKey, defaultValue);
                if(isRequired && StringUtils.isBlank(value)){
                    throw new RuntimeException("Required property '" + configKey + "' is not set.");
                }
                if(StringUtils.isBlank( value)){
                    continue;
                }

                field.setAccessible(true);
                Object convertedValue = convertValue(value, field.getType());
                field.set(obj, convertedValue);
            }
            return obj;
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        }

    }


    /**
     * 将字符串值转换为目标类型
     */
    private Object convertValue(String value, Class<?> targetType) {
        if (targetType == String.class) {
            return value;
        } else if (targetType == int.class || targetType == Integer.class) {
            return Integer.parseInt(value);
        } else if (targetType == boolean.class || targetType == Boolean.class) {
            return Boolean.parseBoolean(value);
        } else if (targetType == long.class || targetType == Long.class) {
            return Long.parseLong(value);
        } else if (targetType == double.class || targetType == Double.class) {
            return Double.parseDouble(value);
        } else if (targetType == float.class || targetType == Float.class) {
            return Float.parseFloat(value);
        } else {
            throw new IllegalArgumentException("不支持的类型转换: " + targetType.getName());
        }
    }

    // 使用示例
    public static void main(String[] args) {
        try {
            ConfigPropertyMapping mapper = new ConfigPropertyMapping("ServerConfig.properties");
            AppConfig config = mapper.mapToObject(AppConfig.class);
            System.out.println("配置信息: " + config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
